package com.apes.crm.customerCar.repository;


import com.apes.crm.car.model.CarBase;
import com.apes.crm.customerCar.model.CustomerCar;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.party.model.Party;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface CustomerCarRepository extends CustomRepository<CustomerCar, String> {


    /***
     * 通过车牌号码和车辆冻结状态查车
     * @param plateNumber
     * @param carFrostStatusCode
     * @return
     */
    CustomerCar findFirstByPlateNumberAndCarFrostStatusCode(String plateNumber, String carFrostStatusCode);

    CustomerCar findFirstByPartyEqualsAndCarEquals(Party party, CarBase carBase);

    List<CustomerCar> findByPartyEqualsAndCarEquals(Party party, CarBase carBase);


    /*
    * 同步会员车辆信息时,更新车辆在其他会员下的车辆冻结状态为冻结,更新版本号
    * */
    @Modifying
    @Transactional
    @Query(
            value = "UPDATE CRM_CUSTOMER_CAR  SET CAR_FROST_STATUS = '000225', VERSION = VERSION + 1 WHERE CAR_ID = ?1 AND PARTY_ID <> ?2",
            nativeQuery = true
    )
    int updateCustomerCarFrost(String carId, long partyId);



}
